#!/usr/bin/env python

import sys
import re
from datetime import datetime

QUEUE = 3


names = []
offset_sec = 0
offset_nsec = 0

prev_stamp = 0

def addnes (type, logfilename, logline, matches):

   global prev_stamp

   global names
   global offset_sec
   global offset_nsec

   for match in matches:
      #sys.stderr.write('match:\'%s\'\n' % match)

      result = re.search(match, logline)
      if result:
         name = result.group(0)

   if name:
      if name not in names:
         names.append(name)
         print('NAM %d %d %s' %(type, (type * 1000 + names.index(name)), '[' + logfilename + ']' + name.replace(' ', '_')))

      #attempt to detect multiple timestamp formats

      #150610-22:42:34.855932
      tstamp = re.findall("\d{6}-\d{2}:\d{2}:\d{2}\.\d+", logline)
      if tstamp:
         if tstamp[0]:
            t = datetime.strptime(tstamp[0], '%y%m%d-%H:%M:%S.%f')
            sec = int((t - datetime(1970, 1, 1)).total_seconds())
            nsec = int(t.microsecond * 1000)
            stamp = long(sec * 1000000000 + nsec)

      else:
         #20150610 22:41:36.082
         tstamp = re.findall("\d{8} \d{2}:\d{2}:\d{2}\.\d+", logline)
         if tstamp:
            if tstamp[0]:
               t = datetime.strptime(tstamp[0], '%Y%m%d %H:%M:%S.%f')
               sec = int((t - datetime(1970, 1, 1)).total_seconds())
               nsec = int(t.microsecond * 1000)
               stamp = long(sec * 1000000000 + nsec)

         else:
            #DAWN 08-20T22:16:45.281Z
            tstamp = re.findall("DAWN \d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+", logline)
            if tstamp:
               if tstamp[0]:
                  t = datetime.strptime('2015-' + tstamp[0][5:], '%Y-%m-%dT%H:%M:%S.%f')
                  sec = int((t - datetime(1970, 1, 1)).total_seconds())
                  nsec = int(t.microsecond * 1000)
                  stamp = long(sec * 1000000000 + nsec)

            else:
               #2015-06-10T20:04:27.941
               tstamp = re.findall("\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+", logline)
               if tstamp:
                  if tstamp[0]:
                     t = datetime.strptime(tstamp[0], '%Y-%m-%dT%H:%M:%S.%f')
                     sec = int((t - datetime(1970, 1, 1)).total_seconds())
                     nsec = int(t.microsecond * 1000)
                     stamp = long(sec * 1000000000 + nsec)

               else:
                  # 1434754719      168720
                  tstamp = re.findall("\d{8,}[ \t]d+", logline)

                  if tstamp:
                     if tstamp[0]:
                        pat = re.compile("\s*")
                        fields = pat.split(tstamp[0])
                        sec = int(fields[0])
                        nsec = int(fields[1]) * 1000
                        stamp = long(sec * 1000000000 + nsec)
                        #sys.stderr.write('sec %d nsec %d \n' % (sec, nsec))

                  else:
                     # 00:15:12.023290
                     tstamp = re.findall("\d{2}:\d{2}:\d{2}.\d+", logline)
                     if tstamp:
                        if tstamp[0]:

                           t = datetime.strptime('2015' + '-' + 
                                                  str(datetime.now().month) + '-' +
                                                  str(datetime.now().day) + '-' +
                                                  tstamp[0], '%Y-%m-%d-%H:%M:%S.%f')

                           sec = int((t - datetime(1970, 1, 1)).total_seconds())
                           nsec = int(t.microsecond * 1000)
                           stamp = long(sec * 1000000000 + nsec)

      if (tstamp):
         if (offset_sec == 0):
            offset_sec = sec
            offset_nsec = nsec

         if prev_stamp:
            if stamp < prev_stamp:
               sys.stderr.write('stamp < prev_stamp\n')

         prev_stamp = long(stamp)

         print('OCC %d %d %d' %(type, (type * 1000 + names.index(name)), (sec * 1000000000 + nsec) - (offset_sec * 1000000000 + offset_nsec)))
         print('DSC 0 0 %s' % logline.replace(" ", "_").replace("\t", "_"))

      else:
         sys.stderr.write('no timestamp found, skipping logline: \'%s\'\n' % logline)


def main():

   offset_sec = 0

   if len(sys.argv) >= 2:

      matches = []
      if len(sys.argv) == 2:
         matches = ['.+']
         totalmatch = '.+'
      else:
         for arg in sys.argv[2:]:
            matches.append(arg)

         totalmatch = ''
         for match in matches:
            totalmatch = totalmatch + '.*' + match + '.*|'

         totalmatch = totalmatch[:-1]

      #sys.stderr.write('totalmatch:\'%s\'\n' % totalmatch)

      loglinematches = []

      print 'TIME 1000000000'
      print 'SPEED 1000000000'


      print('NAM %d %d %s' %(QUEUE, QUEUE * 1000, '[' + sys.argv[1] + ']' ))

      prev_value = 0

      f = open(sys.argv[1])
      for line in f:

         words = line.split(',')

         if re.match(r'\d+', words[0]):

            #print words
            sec = int(words[0]) / 1000

            if offset_sec == 0:
               offset_sec = sec

            value = int(re.search(r'\d+', words[1]).group(0))

            if (value >= prev_value):
               print 'STA %d %d %d %d' %(QUEUE, QUEUE * 1000 + 0, (sec - offset_sec) * 1000000000, value - prev_value)
            else:
               print 'STO %d %d %d %d' %(QUEUE, QUEUE * 1000 + 0, (sec - offset_sec) * 1000000000, prev_value - value)
            #sprintf(fullentry, "STO %d %d %d %d\n", QUEUE, QUEUE * 1000 + 0, sec * 1000000000, 100
            prev_value = value

      f.close()

      print 'END %d UTC' % (offset_sec * 1000000000), datetime.utcfromtimestamp(offset_sec).strftime('%Y-%m-%d %H:%M:%S')

   else:

      print("Usage: %s csvfile [regexp1] [regexp2]..." % sys.argv[0])

if __name__ == "__main__":
   main()